package com.ckerchina.admin.ckadmin.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ckerchina.admin.ckadmin.cache.MemcachedService;
import com.ckerchina.admin.ckadmin.domain.WXUser;

@Repository
public class WxUserDAO extends AbstractDao<WXUser, String>{

	private String namespace = WXUser.class.getName();

	@Autowired
	private MemcachedService memcachedService;


	public WXUser merge(WXUser appUser) {
		WXUser wxUser = this.find(appUser.getUnionid());
		if(wxUser==null){
			this.persist(appUser);
			return appUser;
		}else{
			return super.merge(appUser);
		}
	}
	
	public WXUser findByUserID(long userid) {
		WXUser wxUser = new WXUser();
		List<WXUser> list = this.entityManager
				.createQuery("SELECT o FROM WXUser o WHERE o.userid = ?", WXUser.class)
				.setParameter(1, userid).getResultList();
		if(list!=null && list.size()>0){
			wxUser = list.get(0);
		}
		return wxUser;
	}
}
